require 'MVVM.Sources.View.Adaptor'

InputFieldAdaptor = class('InputFieldAdaptor', Adaptor)

function InputFieldAdaptor:ctor(gameObject, propertyName, binder, func)
    self.component = gameObject:GetComponent('UnityEngine.UI.InputField')
    self.func = func
    self:BindProperty(propertyName, binder)

    binder:RegisterEvent(function(viewModel, property)
        self.component.onEndEdit:AddListener(function(text)
            local modelText = viewModel[propertyName]
            modelText.Value = text
        end)
    end, function()
        self.component.onValueChanged:RemoveAllListeners()
    end, callBack)
end


function InputFieldAdaptor:OnViewModelValueChanged(oldValue, newValue)
    if newValue == nil or newValue == self.component.text then
        return
    end
    if self.func == nil then
        self.component.text = tostring(newValue)
    else
        self.component.text = self.func(newValue)
    end
end

